using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Hotran.SaveSystem {
    public class CheckPoint : MonoBehaviour, ISavable
    {
        private struct SaveData
        {
            public bool Saved;
        }

        private SaveManager _saveManager;
        private SpriteRenderer _renderer;

        private bool _saved = false;


        private void Awake()
        {
            _saveManager = FindObjectOfType<SaveManager>();
            _renderer = GetComponent<SpriteRenderer>();

            UpdateVisual();
        }

        public object Save()
        {
            return new SaveData()
            {
                Saved = _saved
            };
        }

        public void Load(object data)
        {
            SaveData saveData = (SaveData)data;
            _saved = saveData.Saved;
            UpdateVisual();
        }

        private void UpdateVisual()
        {
            _renderer.color = _saved ? Color.green : Color.gray;
        }

        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (_saved)
                return;

            if (collision.CompareTag("Player"))
            {
                _saved = true;
                _saveManager.SaveCurrent();
                UpdateVisual();
            }
        }
    }
}

